package com.ruoyi.web.cp.service.merge;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.web.core.config.ConfigLoader;
import com.ruoyi.web.cp.model.Game;
import com.ruoyi.web.cp.service.ObService;
import com.ruoyi.web.cp.service.RmService;
import com.ruoyi.web.cp.service.rm.RmobDoubleMergeService;
import com.ruoyi.web.cp.service.rm.RmobDx2MergeService;
import com.ruoyi.web.cp.service.rm.RmobDyMergeService;
import com.ruoyi.web.cp.service.rm.RmobMergeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static com.ruoyi.web.constant.Constants.RM_OB_HALF_MERGE;
import static com.ruoyi.web.constant.Constants.RM_OB_MERGE;
import static com.ruoyi.web.cp.service.merge.ZbConstant.*;

@Slf4j
@Service
public class MergeRmobService {
    @Autowired
    private ConfigLoader configLoader;
    @Autowired
    private RmService rmService;
    @Autowired
    private ObService obService;
    @Autowired
    private RmobDoubleMergeService rmobDoubleMergeService;
    @Autowired
    private RmobDyMergeService rmobDyMergeService;
    @Autowired
    private RmobMergeService rmobMergeService;
    @Autowired
    private RmobDx2MergeService rmobDx2MergeService;

    public void mergeRmOb() {
        log.info("======> 合并rmob开始");
        long beginTime = System.currentTimeMillis();

        String rmGamePath = configLoader.getRmPath() + "rm-gameScore.txt";
        String rmDx2GamePath = configLoader.getRmPath() + "rm-gameScore-sou5.txt";
        String rmHalfGamePath = configLoader.getRmPath() + "rm-gameScore-half.txt";

        String obGamePath = configLoader.getRmPath() + "ob-gameScore.txt";
        String obHalfGamePath = configLoader.getRmPath() + "ob-gameScore-half.txt";
        String obAccurateGamePath = configLoader.getRmPath() + "ob-gameScore-accurate.txt";
        String obDoubleGamePath = configLoader.getRmPath() + "ob-gameScore-double.txt";
        String obDyGamePath = configLoader.getRmPath() + "ob-gameScore-dy.txt";
        String obDx2GamePath = configLoader.getRmPath() + "ob-gameScore-dx2.txt";

        List<Game> rmGames = Game.getGames(rmGamePath, "");
        List<Game> rmGames2 = Game.getGames(rmGamePath, "");
        List<Game> rmGamesDouble = Game.getGames(rmGamePath, "");
        List<Game> rmDyGames = Game.getGames(rmGamePath, "");
        List<Game> rmDx2Games = Game.getGames(rmDx2GamePath, "");
        List<Game> rmHalfGames = Game.getGames(rmHalfGamePath, "");

        List<Game> obGames = Game.getGames(obGamePath, "ob");
        List<Game> obHalfGames = Game.getGames(obHalfGamePath, "ob");
        List<Game> obAccurateGames = Game.getGames(obAccurateGamePath, "ob");
        List<Game> obDoubleGames = Game.getGames(obDoubleGamePath, "ob");
        List<Game> obDyGames = Game.getGames(obDyGamePath, "ob");
        List<Game> obDx2Games = Game.getGames(obDx2GamePath, "ob");

        Game.calc(rmGames, obGames, null, "rm");
        Game.calc(rmGames2, obAccurateGames, "accurate", "rm");
        Game.calc(rmGamesDouble, obDoubleGames, "double", "rm");
        Game.calc(rmDyGames, obDyGames, "dy", "rm");
        Game.calc(rmDx2Games, obDx2Games, "dx2", "rm");
        Game.calc(rmHalfGames, obHalfGames, null, "rm");

        writeMergeResult(rmGames, configLoader.getRmPath(), "rmob-merge");
        writeMergeResult(rmGames2, configLoader.getRmPath(), "rmob-merge-accurate");
        writeMergeResult(rmGamesDouble, configLoader.getRmPath(), "rmob-merge-double");
        writeMergeResult(rmDyGames, configLoader.getRmPath(), "rmob-merge-dy");
        writeMergeResult(rmDx2Games, configLoader.getRmPath(), "rmob-merge-dx2-");
        writeMergeResult(rmHalfGames, configLoader.getRmPath(), "rmob-merge-half");

        log.info(StrUtil.format("======> 合并rmob结束,耗时:{}", (System.currentTimeMillis() - beginTime)));
    }

    private void writeMergeResult(List<Game> games, String path, String name) {
        List<String> allGames = new ArrayList<>();
        List<String> fitGames = new ArrayList<>();
        List<String> fitGames30 = new ArrayList<>();
        List<String> fitGames150 = new ArrayList<>();
        List<String> fitGames100 = new ArrayList<>();
        List<String> fitGamesDouble = new ArrayList<>();
        List<String> fitGamesDouble2 = new ArrayList<>();
        List<String> fitGamesDy = new ArrayList<>();
        List<String> fitGamesDx2 = new ArrayList<>();
        List<String> noMatch = new ArrayList<>(); //未匹配

        for (Game game : games) {
            try {
                StringBuilder sb = new StringBuilder();
                sb.append(game.getDateStr()).append("\t");
                sb.append(game.getLeague()).append("\t");
                sb.append(game.getTeamA()).append("\t");
                sb.append(game.getTeamB()).append("\t");
                sb.append(game.getGameId());
                if (game.getMemo() != null) {
                    sb.append("\t").append(game.getMemo());
                    noMatch.add(sb.toString());
                }
                allGames.add(sb.toString());
                List<String> lists = new ArrayList<>();
                List<String> lists30 = new ArrayList<>();
                List<String> lists150 = new ArrayList<>();
                List<String> lists100 = new ArrayList<>();
                List<String> listsDouble = new ArrayList<>();
                List<String> listsDouble2 = new ArrayList<>();
                List<String> listsDy = new ArrayList<>();
                List<String> listsDx2 = new ArrayList<>();

                for (Game.Score score : game.getScores()) {
                    StringBuilder sb1 = new StringBuilder();
                    sb1.append(score.getScore()).append("\t");
                    sb1.append(score.getPlv()).append("\t");
                    if (score.getPlv2() != null) {
                        sb1.append(score.getPlv2()).append("\t");
                    }
                    if (score.getPlv3() != null) {
                        sb1.append(score.getPlv3()).append("\t");
                    }
                    if (score.getPlv4() != null) {
                        sb1.append(score.getPlv4()).append("\t");
                    }
                    String fit = score.getFit();
                    String plv = score.getPlv();
                    String plv2 = score.getPlv2();
                    String doublePlv = score.getDoublePlv();
                    String str = sb1.toString();
                    String str2 = sb1.toString();

                    //记录利润>=0
                    if (fit != null && Double.valueOf(fit) > 0) {
                        String s = str + "收：" + fit;
                        lists.add(s.replace("\t", "|"));
                    }
                    if (name.contains("double")) {
                        if (doublePlv != null) {
                            str = str + doublePlv
                                    + "\t" + score.getBdan00Plv()
                                    + "\t" + score.getDrawPlv();
                            str2 = str2 + doublePlv
                                    + "\t" + score.getBdan00Plv()
                                    + "\t" + score.getGoalDesc()
                                    + "\t" + score.getGoalBigPlv()
                                    + "\t" + score.getGoalSmallPlv()
                                    + "\t" + score.getWinPlv()
                                    + "\t" + score.getDrawPlv()
                                    + "\t" + score.getLosePlv()
                                    + "\t" + score.getAsianDesc()
                                    + "\t" + score.getAsianBigPlv()
                                    + "\t" + score.getAsianSmallPlv()
                            ;
                            allGames.add(str);
                        }
                    } else if (name.contains("dy")) {
                        if (doublePlv != null) {
//                            str = str + "\t" + plv3;
                            allGames.add(str);
                        }
                    } else if (name.contains("dx2")) {
                        if (score.getGoalDesc() != null) {
                            str = str + score.getGoalDesc() + "\t" + score.getBdan00Plv();
                            allGames.add(str);
                        }
                    } else {
                        allGames.add(str + fit);
                    }

                    //记录利润>=30
                    if (fit != null && Double.parseDouble(fit) >= MIN_FIT) {
                        String s = str + "收：" + fit;
                        lists30.add(s.replace("\t", "|"));
                    }
                    //记录正波>=150
                    if (plv2 != null && Double.parseDouble(plv2) >= MAX_ZB_PLV_150 && !name.contains("accurate")) {
                        String s = str + "收：" + fit;
                        lists150.add(s.replace("\t", "|"));
                    }
                    //记录正波>=100
                    if (plv2 != null && Double.parseDouble(plv2) >= MAX_ZB_PLV_100 && !name.contains("accurate")) {
                        String s = str + "收：" + fit;
                        lists100.add(s.replace("\t", "|"));
                    }
                    if (name.contains("double") && doublePlv != null && Double.parseDouble(doublePlv) <= MIN_PLV_DOUBLE) {
                        listsDouble.add(str.replace("\t", "|"));
                        listsDouble2.add(str2.replace("\t", "|"));
                    }
                    if (name.contains("dy") && plv2 != null && Double.parseDouble(plv2) >= MIN_PLV_DY) {
                        listsDy.add(str.replace("\t", "|"));
                    }
                    if (name.contains("dx2") && plv != null && score.getGoalDesc() != null) {
                        listsDx2.add(str.replace("\t", "|"));
                    }
                    if ("双进".equals(score.getScore())) {
                        String s = "OB双进:" + score.getPlv();
                        if (CollUtil.isNotEmpty(lists100)) {
                            lists100.add(s);
                        }
                        if (CollUtil.isNotEmpty(lists150)) {
                            lists150.add(s);
                        }
                    }
                    if ("大小".equals(score.getScore())) {
                        String s = "OB大小:" + score.getPlv();
                        if (CollUtil.isNotEmpty(lists100)) {
                            lists100.add(s);
                        }
                        if (CollUtil.isNotEmpty(lists150)) {
                            lists150.add(s);
                        }
                    }
                    if ("亚盘".equals(score.getScore())) {
                        String s = "OB亚盘:" + score.getPlv();
                        if (CollUtil.isNotEmpty(lists100)) {
                            lists100.add(s);
                        }
                        if (CollUtil.isNotEmpty(lists150)) {
                            lists150.add(s);
                        }
                    }
                }
                if (CollUtil.isNotEmpty(lists)) {
                    fitGames.add(sb.toString().replace("\t", ","));
                    fitGames.addAll(lists);
                }
                if (CollUtil.isNotEmpty(lists30)) {
                    fitGames30.add(sb.toString().replace("\t", ","));
                    fitGames30.addAll(lists30);
                }
                if (CollUtil.isNotEmpty(lists150)) {
                    fitGames150.add(sb.toString().replace("\t", ","));
                    fitGames150.addAll(lists150);
                }
                if (CollUtil.isNotEmpty(lists100)) {
                    fitGames100.add(sb.toString().replace("\t", ","));
                    fitGames100.addAll(lists100);
                }
                if (CollUtil.isNotEmpty(listsDouble)) {
                    fitGamesDouble.add(sb.toString().replace("\t", ","));
                    fitGamesDouble.addAll(listsDouble);
                }
                if (CollUtil.isNotEmpty(listsDouble2)) {
                    fitGamesDouble2.add(sb.toString().replace("\t", ","));
                    fitGamesDouble2.addAll(listsDouble2);
                }
                if (CollUtil.isNotEmpty(listsDy)) {
                    fitGamesDy.add(sb.toString().replace("\t", ","));
                    fitGamesDy.addAll(listsDy);
                }
                if (CollUtil.isNotEmpty(listsDy)) {
                    fitGamesDy.add(sb.toString().replace("\t", ","));
                    fitGamesDy.addAll(listsDy);
                }
                if (CollUtil.isNotEmpty(listsDx2)) {
                    fitGamesDx2.add(sb.toString().replace("\t", ","));
                    fitGamesDx2.addAll(listsDx2);
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }
        FileUtil.writeLines(allGames, path + name + ".txt", "utf-8");
        FileUtil.writeLines(fitGames, path + name + "2.txt", "utf-8");
        FileUtil.writeLines(fitGames30, path + name + "30.txt", "utf-8");
        FileUtil.writeLines(fitGames150, path + name + "150.txt", "utf-8");
        FileUtil.writeLines(fitGames100, path + name + "100.txt", "utf-8");
        FileUtil.writeLines(fitGamesDouble, path + name + "1.5.txt", StandardCharsets.UTF_8);
        FileUtil.writeLines(fitGamesDouble2, path + name + "1.5-2.txt", StandardCharsets.UTF_8);
        FileUtil.writeLines(fitGamesDy, path + name + "7.txt", StandardCharsets.UTF_8);
        FileUtil.writeLines(fitGamesDx2, path + name + "5.txt", StandardCharsets.UTF_8);
        FileUtil.writeLines(noMatch, path + "merge" + System.getProperty("file.separator") + "rmob-match" + ".txt", "utf-8");

        rmobDoubleMergeService.saveRmobDoubleMerge(fitGamesDouble2);
        rmobDyMergeService.saveRmobDyMerge(fitGamesDy);
        rmobDx2MergeService.saveRmobDx2Merge(fitGamesDx2);
        if ("rmob-merge".equals(name)) {
            rmobMergeService.saveRmobMerge(fitGames100, RM_OB_MERGE);
        }
        if ("rmob-merge-half".equals(name)) {
            rmobMergeService.saveRmobMerge(fitGames100, RM_OB_HALF_MERGE);
        }
    }

    public Set<String> getMergeGameNames(String path) {
        List<String> strs = FileUtil.readLines(path, "utf-8");
        Set<String> set = new HashSet<>();
        for (String str : strs) {
            String[] split = str.split(",");
            if (split.length >= 3) {
                set.add(split[2].split("vs")[0].trim());
            }
        }
        return set;
    }
}
